package com.ys.nms.module.business.service.alertRules;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ys.nms.module.business.controller.admin.alertRules.vo.*;
import com.ys.nms.module.business.dal.dataobject.alertRules.AlertRulesDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.alertRules.AlertRulesMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 报警规则 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class AlertRulesServiceImpl implements AlertRulesService {

    @Resource
    private AlertRulesMapper alertRulesMapper;

    @Override
    public Integer createAlertRules(AlertRulesSaveReqVO createReqVO) {
        // 插入
        AlertRulesDO alertRules = BeanUtils.toBean(createReqVO, AlertRulesDO.class);
        alertRulesMapper.insert(alertRules);

        // 返回
        return alertRules.getId();
    }

    @Override
    public void updateAlertRules(AlertRulesSaveReqVO updateReqVO) {
        // 校验存在
        validateAlertRulesExists(updateReqVO.getId());
        // 更新
        AlertRulesDO updateObj = BeanUtils.toBean(updateReqVO, AlertRulesDO.class);
        alertRulesMapper.updateById(updateObj);
    }

    @Override
    public void deleteAlertRules(Integer id) {
        // 校验存在
        validateAlertRulesExists(id);
        // 删除
        alertRulesMapper.deleteById(id);
    }

    @Override
        public void deleteAlertRulesListByIds(List<Integer> ids) {
        // 删除
        alertRulesMapper.deleteByIds(ids);
        }


    private void validateAlertRulesExists(Integer id) {
        if (alertRulesMapper.selectById(id) == null) {
            throw exception(ALERT_RULES_NOT_EXISTS);
        }
    }

    @Override
    public AlertRulesDO getAlertRules(Integer id) {
        return alertRulesMapper.selectById(id);
    }

    @Override
    public PageResult<AlertRulesDO> getAlertRulesPage(AlertRulesPageReqVO pageReqVO) {
        return alertRulesMapper.selectPage(pageReqVO);
    }

}